package edu.pku.sei.modeler.uml.commands.compositestructure;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.uml.Collaborations.CollaborationUse;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.Kernel.NamedElement;
import edu.pku.sei.modeler.uml.commands.ConnectionModelCreateCommand;

public class RoleBindingCreateCommand extends ConnectionModelCreateCommand {
	private CollaborationUse source;
	private NamedElement target;
	private Dependency dependency;
	
	public RoleBindingCreateCommand(MetaModelElement connectionModel,
			MetaModelElement sourceModel) {
		super(connectionModel, sourceModel);
	}
	
	@Override
	public boolean canExecute() {
		if (sourceModel instanceof CollaborationUse && targetModel instanceof NamedElement)
			return super.canExecute();
		else return false;
	}
	
	@Override
	public void execute() {
		source = (CollaborationUse) this.sourceModel;
		target = (NamedElement) this.targetModel;
		dependency = (Dependency) this.connectionModel;
		
		dependency.getClientDependency().add(target);
		source.getRoleBinding().add(dependency);
		
	}
	
	@Override
	public void undo() {
		dependency.getClientDependency().remove(target);
		source.getRoleBinding().remove(dependency);
	}

}
